package com.jbm.jbmsupplier.activity.address;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.AllActivityApp;
import com.jbm.jbmsupplier.activity.repairs.FixCategoryAty;
import com.jbm.jbmsupplier.activity.repairs.UserAddress;
import com.jbm.jbmsupplier.net.NetAddRess;
import com.jbm.jbmsupplier.utlis.ExitActivity;
import com.jbm.jbmsupplier.zp.entity.PullMessage;
import com.jbm.jbmsupplier.zp.utils.FileUtils;
import com.jbm.onehome.iface.ICertAddress;
import com.jbm.onehome.iface.IGeoinfoCtrl;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


/**
 * 添加地址页面
 */
public class AddressEditActivity extends AllActivityApp {

    public static final int ADDRESSMESAGE = 0001;
    private final int EXIT = 0002;


    @InjectView(R.id.address_name)
    EditText addressName;
    @InjectView(R.id.address_mobile)
    EditText addressMobile;
    @InjectView(R.id.address_selectAddress)
    Spinner addressSelectAddress;
    @InjectView(R.id.address_selectXiaoQu)
    Spinner addressSelectXiaoQu;

    @InjectView(R.id.address_selectLouMen)
    Spinner addressSelectLouMen;
    @InjectView(R.id.ln_louMen)
    LinearLayout lnLouMen;
    @InjectView(R.id.address_selectHuHao)
    Spinner addressSelectHuHao;
    @InjectView(R.id.ln_huHao)
    LinearLayout lnHuHao;
    @InjectView(R.id.address_et)
    EditText addressEt;
    @InjectView(R.id.address_commit)
    ImageView addressCommit;

    TextView headerRight;
    @InjectView(R.id.teleft)
    TextView teleft;
    @InjectView(R.id.tetitle)
    TextView tetitle;
    @InjectView(R.id.toolbar_image)
    ImageView toolbarImage;
    @InjectView(R.id.to_custro)
    Toolbar toCustro;
    @InjectView(R.id.ln_XiaoQu)
    LinearLayout lnXiaoQu;
    private PullMessage pullMessage;

    AddressModel model;
    private int fixCategory;
    private String contacts, mobile, address;

    /**
     * 这个类 需要  add 数据
     *
     * @return
     */

    @Override
    public int setContentView() {
        return R.layout.activity_address_edit;
    }

    @Override
    public void initIalizeView() {
        //从小秘书过来的  将这个对象保存
        pullMessage = (PullMessage) getIntent().getSerializableExtra("mishu");
        //这是保修类型  普通报修为0   其他报修>0   1公租房
        fixCategory = getIntent().getIntExtra("FixCategory", 0);

        ExitActivity.getInstance().addActivity(this);
        ButterKnife.inject(this);
        tetitle.setText("添加地址");
        Spinner[] spArr = new Spinner[]{addressSelectAddress, addressSelectXiaoQu, addressSelectLouMen, addressSelectHuHao};
        model = new AddressModel(this, null, spArr);
        model.setBackListener(new AddressModel.BackListener() {
            @Override
            public void Click(int type, int gi_id) {
                addressEt.setVisibility(View.GONE);
                switch (type) {
                    //代表点击了地区
                    case 1:
                        model.getNetData(0, gi_id, "", "village", "");
                        break;
                    case 2:
                        model.getNetData(0, gi_id, "", "unit", "");
                        break;
                    case 3:
                        model.getNetData(0, gi_id, "", "numbers", "");
                        break;
                }
            }


            @Override
            public void Put(boolean isVisible) {
                initHidden();
                if (isVisible == true) {
                    addressEt.setVisibility(View.VISIBLE);
                } else {
                    addressEt.setVisibility(View.GONE);
                }

            }
        });
    }

    private ICertAddress result;
    //用于记录下表 有直辖市和县
    public FileUtils fileUtils;

    private Handler mHandler = new Handler() {

        public void handleMessage(Message paramMessage) {
            model.dismissLoader();
            switch (paramMessage.what) {
                case ADDRESSMESAGE:
                    result = (ICertAddress) paramMessage.obj;
                    //--------------------------
                    if (result.m_code == 200) {
                        if (model.getHuHaoType().equals("t")) {
                            if (getIntent().getStringExtra("add").equals("2")) {
                                Intent intent = new Intent("Address");
                                intent.putExtra("1", "1");
                                intent.putExtra("gi_text", result.gi_text);
                                intent.putExtra("caddr_id", result.caddr_id);
                                intent.putExtra("method", result.mobile);
                                sendBroadcast(intent);
                            } else if (getIntent().getStringExtra("add").equals("1")) {
                                //  小秘书添加地址  然后回调小秘书使其个获取地址
                                fileUtils = new FileUtils(AddressEditActivity.this);
                                if (pullMessage == null) {
                                    pullMessage = new PullMessage();
                                }
                                ICertAddress[] addressList = new ICertAddress[1];
                                addressList[0] = result;
                                pullMessage.setCa(addressList);
                                fileUtils.Write(pullMessage);
                                Intent intent1 = new Intent("Address");
                                intent1.putExtra("1", "2");
                                sendBroadcast(intent1);
                            } else {
                                Intent intent = new Intent();
                                intent.putExtra("address", result);
                                setResult(RESULT_OK, intent);
                            }
                            finish();
                        } else {
                            Intent intent1 = new Intent(AddressEditActivity.this, FixCategoryAty.class);
                            UserAddress date = new UserAddress();
                            date.id = result.caddr_id;
                            date.mAddress = result.gi_text;
                            date.mContacts = result.contacts;
                            date.mMobile = result.mobile;
                            // //这里指的 c公产房 z公租房 j 我爱我家  t通用
                            //0 通用  1 公产房   2 公租房   3 我爱我家
                            intent1.putExtra("category", model.getHuHaoType());
                            //将地址传到公产房报修页面
                            intent1.putExtra("address", date);
                            startActivity(intent1);

                        }
                    }
                    break;
                //获取地址
                case NetAddRess.ADDRESSCENGJIERROR: {
                    IGeoinfoCtrl result2 = (IGeoinfoCtrl) paramMessage.obj;
                    if (result2.GIlist != null) {
                        model.setDate(result2.GIlist, addressSelectAddress);
                        addressEt.setVisibility(View.GONE);
                    } else {
                        model.setClickType(model.getClickType() - 1);
                        addressEt.setVisibility(View.VISIBLE);
                    }
                    initHidden();
                    break;
                }
            }

        }
    };

    //组建的隐藏和显示
    private void initHidden() {
        switch (model.getClickType()) {
            case 0:
                lnXiaoQu.setVisibility(View.GONE);
                lnLouMen.setVisibility(View.GONE);
                lnHuHao.setVisibility(View.GONE);
                break;
            case 1:
                lnXiaoQu.setVisibility(View.VISIBLE);
                lnLouMen.setVisibility(View.GONE);
                lnHuHao.setVisibility(View.GONE);
                break;
            case 2:
                lnXiaoQu.setVisibility(View.VISIBLE);
                lnLouMen.setVisibility(View.VISIBLE);
                lnHuHao.setVisibility(View.GONE);
                break;
            case 3:
                lnXiaoQu.setVisibility(View.VISIBLE);
                lnLouMen.setVisibility(View.VISIBLE);
                lnHuHao.setVisibility(View.VISIBLE);
                break;
        }
    }


    @OnClick({R.id.teleft, R.id.address_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.teleft:
                setResult(Activity.RESULT_CANCELED);
                finish();
                break;
            case R.id.address_commit:
                contacts = addressName.getText().toString();
                mobile = addressMobile.getText().toString();//电话
                address = model.getAddress(addressEt);
                if (contacts.equals("")) {
                    hint("用户名不能为空");
                } else if (mobile.equals("")) {
                    hint("手机号不能为空");
                } else if (mobile.length() != 11) {
                    hint("请核实您的手机号码");
                } else if (address.isEmpty() || address.length() < 6) {
                    hint("请输入正确的地址");
                } else {
                    //这里是提交地址   代表普通地址
                    if (addressEt.getText().toString() != null && addressEt.getText().toString().length() != 0) {
                        model.setHuHaoType("t");
                    }
                    model.commitAddress(mobile, contacts, model.getHuHaoType());
                }
                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
    }
}
